package com.lagou.controller;

import com.lagou.domain.Course;
import com.lagou.domain.CourseLesson;
import com.lagou.domain.CourseSection;
import com.lagou.domain.ResponseResult;
import com.lagou.service.CourseContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/courseContent")
public class CourseContentController {

    @Autowired
    private CourseContentService courseContentService;


    @RequestMapping("/findSectionAndLesson")
    public ResponseResult findSectionAndLesson(int courseId){
        List<CourseSection> list = courseContentService.findSectionAndLessonByCourseId(courseId);

        ResponseResult result = new ResponseResult(true, 200, "响应成功", list);
        return result;

    }

    @RequestMapping("/findCourseByCourseId")
    public ResponseResult findCourseById(int courseId){
        Course course = courseContentService.findCourseById(courseId);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("id",course.getId());
        map.put("courseName",course.getCourseName());
        ResponseResult result = new ResponseResult(true, 200, "响应成功", map);
        return result;
    }

    @RequestMapping("/saveOrUpdateSection")
    public ResponseResult saveOrUpdateSection(@RequestBody CourseSection courseSection){

        if (courseSection.getId() == null){
            courseContentService.saveSection(courseSection);
            ResponseResult result = new ResponseResult(true, 200, "响应成功", null);
            return result;
        }else {
            courseContentService.updateSection(courseSection);
            ResponseResult result = new ResponseResult(true, 200, "响应成功", null);
            return result;
        }

    }

    @RequestMapping("/updateSectionStatus")
    public ResponseResult updateSectionStatus(Integer id,Integer status){

        courseContentService.updateSectionStatus(id,status);

        HashMap<Object, Object> map = new HashMap<>();
        map.put("status",status);
        ResponseResult result = new ResponseResult(true, 200, "响应成功", map);

        return result;
    }

    @RequestMapping("/saveLesson")
    public ResponseResult saveLesson(@RequestBody CourseLesson courseLesson){
        courseContentService.saveLesson(courseLesson);
        ResponseResult result = new ResponseResult(true, 200, "响应成功", null);
        return result;

    }

}
